﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ePlanet.Entities;
using ePlanet.ArchiveMange.Models;
using Web.SystemConfig.WorkFlow;
using ePlanet.ArchiveMange.Services;
using DreamBuilder.Core.Container;
using Web.SystemConfig.SysCache;
using ePlanet.SingleWell.DataAccess;
using ePlanet.SingleWell.Models;
using Web.Views.DataManage.Models;
using Web.SystemLogin.Business;
using Newtonsoft.Json;

namespace Web.Views.DataManage.PLuploads
{
    public class TemplateProcess
    {
        public void DocumentArchiveProcess1(string requestData, string catalogId, string userId)
        {
            try
            {
                List<Guid> entityIds = new List<Guid>();
                List<DocumentArchiveEntity> entities = Newtonsoft.Json.JsonConvert.DeserializeObject<List<DocumentArchiveEntity>>(requestData);
                ArchiveMangeModal model = new ArchiveMangeModal();
                foreach (DocumentArchiveEntity entity in entities)
                {
                    ArchiveMangeModal.DOCUMENT_ARCHIVERow row = model.DOCUMENT_ARCHIVE.NewDOCUMENT_ARCHIVERow();
                    row.DATAID = entity.DataId;
                    row.TEMPORDERNUM = entity.TempOrderNum;
                    row.TITLE = entity.Title;
                    row.PUBLISHER = entity.Publisher;
                    if (entity.PublishTime.HasValue)
                    {
                        row.PUBLISHTIME = entity.PublishTime.Value;
                    }
                    else
                    {
                        row.SetPUBLISHTIMENull();
                    }
                    row.FILEORGID = entity.FileOrgId;
                    row.F_KEEPTIME = entity.F_KeepTime;
                    row.F_CARRIERTYPE = entity.F_CarrierType;
                    row.F_SECURITYTYPE = entity.F_SecurityType;
                    //TODO
                    if (entity.PageNum.HasValue)
                    {
                        row.PAGENUM = entity.PageNum.Value;
                    }
                    else
                    {
                        row.SetPAGENUMNull();
                    }
                    if (entity.MainBodyNum.HasValue)
                    {
                        row.MAINBODYNUM = entity.MainBodyNum.Value;
                    }
                    else
                    {
                        row.SetMAINBODYNUMNull();
                    }
                    if (entity.DoplicateBodyNum.HasValue)
                    {
                        row.DOPLICATEBODYNUM = entity.DoplicateBodyNum.Value;
                    }
                    else
                    {
                        row.SetDOPLICATEBODYNUMNull();
                    }
                    row.SUMMARY = entity.Summary;
                    row.REMARK = entity.Remark;
                    row.STORAGETYPE = entity.StorageType;
                    row.FILENUM = entity.FileNum;
                    row.CREATEUSERID = userId;
                    row.CREATETIME = DateTime.Now;
                    row.STATUSID = new WorkFlowManager().GetOriginalStastuIDByTableID(ArchiveTableEnum.Document);
                    model.DOCUMENT_ARCHIVE.AddDOCUMENT_ARCHIVERow(row);
                    entityIds.Add(new Guid(entity.DataId));
                }
                model.DOCUMENT_ARCHIVE.AcceptChanges();
                foreach (ArchiveMangeModal.DOCUMENT_ARCHIVERow row in model.DOCUMENT_ARCHIVE.Rows)
                {
                    row.SetAdded();
                }
                IDocumentArchiveService DocumentArchiveService = UContainer.Resolve<IDocumentArchiveService>();
                DocumentArchiveService.SaveData(model);
                foreach (Guid id in entityIds)
                {
                    //this.FlowStart(id, new Guid(userId),ArchiveTableEnum.Document);
                }
            }
            catch (Exception)
            {
                throw;
            }

        }

        public void SingleWellProcess(string requestData, string catalogId, string userId)
        {
            try
            {
                List<Guid> entityIds = new List<Guid>();
                List<SingleWellEntity> singleWells = Newtonsoft.Json.JsonConvert.DeserializeObject<List<SingleWellEntity>>(requestData);
                WellCatalogDAL wellCatalogDAL = new WellCatalogDAL();
                WellCatalog newWellCatalog = new WellCatalog();
                WellCatalog currentWellCatalog = wellCatalogDAL.GetWellCatalogByID(catalogId);
                if (currentWellCatalog == null || currentWellCatalog.WELLCATALOG == null || currentWellCatalog.WELLCATALOG.Rows.Count == 0)
                {
                    throw new Exception("当前编目不存在！");
                }
                if (currentWellCatalog.WELLCATALOG[0].IsISWELLCODENull() || currentWellCatalog.WELLCATALOG[0].ISWELLCODE == 0)
                {
                    throw new Exception("当前编目下不允许创建编目！");
                }
                WellCatalog wellCatalog = wellCatalogDAL.GetWellCatalogByParentID(catalogId);
                foreach (SingleWellEntity entity in singleWells)
                {
                    bool isExist = false;
                    foreach (WellCatalog.WELLCATALOGRow row in wellCatalog.WELLCATALOG.Rows)
                    {
                        if (entity.WellCode == row.CATALOGDESCRIPT)
                        {
                            entity.WellCatalogId = row.WELLCATALOGID;
                            isExist = true;
                            break;
                        }
                    }
                    if (!isExist)
                    {
                        bool flag = false;
                        foreach (WellCatalog.WELLCATALOGRow newRow in newWellCatalog.WELLCATALOG.Rows)
                        {
                            if (newRow.CATALOGDESCRIPT == entity.WellCode)
                            {
                                entity.WellCatalogId = newRow.WELLCATALOGID;
                                flag = true;
                                break;
                            }
                        }

                        if (!flag)
                        {
                            WellCatalog.WELLCATALOGRow row = newWellCatalog.WELLCATALOG.NewWELLCATALOGRow();
                            row.WELLCATALOGID = string.Concat(Guid.NewGuid());
                            row.CATALOGDESCRIPT = entity.WellCode;
                            row.PARENTID = catalogId;
                            row.CATELOGSORT = "1";
                            row.ISWELLCODE = 0;
                            entity.WellCatalogId = row.WELLCATALOGID;
                            newWellCatalog.WELLCATALOG.AddWELLCATALOGRow(row);
                        }
                    }
                }
                newWellCatalog.WELLCATALOG.AcceptChanges();
                foreach (WellCatalog.WELLCATALOGRow row in newWellCatalog.WELLCATALOG.Rows)
                {
                    row.SetAdded();
                }
                wellCatalogDAL.AddNewCatalog(newWellCatalog);

                #region"SingleWellDataToRow"
                SingleWellModel sModel = new SingleWellModel();
                foreach (SingleWellEntity singleWellEntity in singleWells)
                {
                    SingleWellModel.SINGLEWELLRow row = sModel.SINGLEWELL.NewSINGLEWELLRow();
                    SingleWellEntityHelper.EntityToRow(singleWellEntity, row);
                    row.CREATEUSERID = userId;
                    row.INSERTTIME = DateTime.Now;
                    row.WELLCATALOGID = singleWellEntity.WellCatalogId;
                    //row.STATUSID = new WorkFlowManager().GetOriginalStastuIDByTableID(ArchiveTableEnum.SingleWell);
                    row.STATUSID = new ArchiveCacheFacade().GetStatusRuleCache().GetFinishStatus((int)ArchiveTableEnum.SingleWell);
                    row.CATEGORY = singleWellEntity.Category;
                    row.CATEGORYDETAIL = singleWellEntity.CategoryDetail;
                    foreach (SingleWellDetailEntity detail in singleWellEntity.SingleWellDetailEntitys)
                    {
                        SingleWellModel.WELLDATADETAILRow detailRow = sModel.WELLDATADETAIL.NewWELLDATADETAILRow();
                        detailRow.DETAILID = detail.DetailID;
                        detailRow.WELLDATAID = detail.WellDataID;
                        detailRow.DETAILNAME = detail.DataFileName;
                        if (detail.DetailPageNumber == null)
                        {
                            detailRow.SetPAGENUMNull();
                        }
                        else
                        {
                            detailRow.PAGENUM = detail.DetailPageNumber.Value;
                        }
                        if (detail.A4 == null)
                        {
                            detailRow.SetA4Null();
                        }
                        else
                        {
                            detailRow.A4 = detail.A4.Value;
                        }
                        if (detail.A3 == null)
                        {
                            detailRow.SetA3Null();
                        }
                        else
                        {
                            detailRow.A3 = detail.A3.Value;
                        }
                        if (detail.A0 == null)
                        {
                            detailRow.SetA0Null();
                        }
                        else
                        {
                            detailRow.A0 = detail.A0.Value;
                        }
                        sModel.WELLDATADETAIL.AddWELLDATADETAILRow(detailRow);
                    }
                    sModel.SINGLEWELL.AddSINGLEWELLRow(row);
                    //entityIds.Add(new Guid(singleWellEntity.DataId));
                }
                sModel.SINGLEWELL.AcceptChanges();
                sModel.WELLDATADETAIL.AcceptChanges();
                foreach (SingleWellModel.SINGLEWELLRow row in sModel.SINGLEWELL.Rows)
                {
                    row.SetAdded();
                }
                foreach (SingleWellModel.WELLDATADETAILRow detailRow in sModel.WELLDATADETAIL.Rows)
                {
                    detailRow.SetAdded();
                }
                SingleWellDAl singleWellDAL = new SingleWellDAl();
                singleWellDAL.AddNewSingleWellModel(sModel);
                //foreach (Guid id in entityIds)
                //{
                //    this.FlowStart(id, new Guid(userId),ArchiveTableEnum.SingleWell);
                //}
                #endregion
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void ArchiveProcess(string requestData, string catalogId, string userId)
        {
            try
            {
                BUSSystemLogin busSystemLogin = new BUSSystemLogin();
                string orgFullFonds = busSystemLogin.GetOrgFullFonds(userId);

                ArchiveMangeModal archiveMangeModal = new ArchiveMangeModal();
                List<Guid> entityIds = new List<Guid>();
                //ArchiveTableEnum archiveTableEnum;
                List<string> updates = new List<string>();
                switch (catalogId)
                {
                    case "1": //文书类 W
                        //archiveTableEnum = ArchiveTableEnum.Document;
                        UpdateArchiveAndPageFileModel<DocumentArchiveEntity, PageFileEntity> model = Newtonsoft.Json.JsonConvert.DeserializeObject<UpdateArchiveAndPageFileModel<DocumentArchiveEntity, PageFileEntity>>(requestData);
                        IDocumentArchiveService DocumentArchiveService = UContainer.Resolve<IDocumentArchiveService>();
                        foreach (DocumentArchiveEntity entity in model.Archives)
                        {
                            ArchiveMangeModal.DOCUMENT_ARCHIVERow row = archiveMangeModal.DOCUMENT_ARCHIVE.NewDOCUMENT_ARCHIVERow();
                            DocumentArchiveEntityHelper.EntityToRow(entity, row);
                            row.CREATEUSERID = userId;
                            row.CREATETIME = DateTime.Now;
                            row.FULLFONDSNUM = orgFullFonds;
                            if (!string.IsNullOrEmpty(entity.ArchiveNum))
                            {
                                row.STATUSID = new ArchiveCacheFacade().GetStatusRuleCache().GetFinishStatus(int.Parse(catalogId));
                            }
                            else
                            {
                                //row.STATUSID = new WorkFlowManager().GetOriginalStastuIDByTableID(archiveTableEnum);
                                row.STATUSID = 0;
                                entityIds.Add(new Guid(entity.DataId));
                            }
                            archiveMangeModal.DOCUMENT_ARCHIVE.AddDOCUMENT_ARCHIVERow(row);
                            if (entity.DataRowOpMode == "Update")
                            {
                                updates.Add(entity.DataId);
                            }
                        }
                        archiveMangeModal.AcceptChanges();
                        foreach (ArchiveMangeModal.DOCUMENT_ARCHIVERow row in archiveMangeModal.DOCUMENT_ARCHIVE.Rows)
                        {
                            if (updates.Contains(row.DATAID))
                            {
                                row.SetModified();
                            }
                            else
                            {
                                row.SetAdded();
                            }
                        }
                        DocumentArchiveService.SaveData(archiveMangeModal);
                        break;
                    case "2": //光盘
                        //archiveTableEnum = ArchiveTableEnum.AudioVedio;
                        UpdateArchiveAndPageFileModel<CdArchiveEntity, PageFileEntity> model9 = Newtonsoft.Json.JsonConvert.DeserializeObject<UpdateArchiveAndPageFileModel<CdArchiveEntity, PageFileEntity>>(requestData);
                        ICDArchiveService CDArchiveService = UContainer.Resolve<ICDArchiveService>();
                        foreach (CdArchiveEntity entity in model9.Archives)
                        {
                            ArchiveMangeModal.CD_ARCHIVERow row = archiveMangeModal.CD_ARCHIVE.NewCD_ARCHIVERow();
                            CdArchiveEntityHelper.EntityToRow(entity, row);
                            row.CREATEUSERID = userId;
                            row.CREATETIME = DateTime.Now;
                            row.FULLFONDSNUM = orgFullFonds;
                            if (!string.IsNullOrEmpty(entity.ArchiveNum))
                            {
                                row.STATUSID = new ArchiveCacheFacade().GetStatusRuleCache().GetFinishStatus(int.Parse(catalogId));
                            }
                            else
                            {
                                //row.STATUSID = new WorkFlowManager().GetOriginalStastuIDByTableID(archiveTableEnum);
                                row.STATUSID = 0;
                                entityIds.Add(new Guid(entity.DataId));
                            }
                            archiveMangeModal.CD_ARCHIVE.AddCD_ARCHIVERow(row);
                            if (entity.DataRowOpMode == "Update")
                            {
                                updates.Add(entity.DataId);
                            }
                        }
                        archiveMangeModal.AcceptChanges();
                        foreach (ArchiveMangeModal.CD_ARCHIVERow row in archiveMangeModal.CD_ARCHIVE.Rows)
                        {
                            if (updates.Contains(row.DATAID))
                            {
                                row.SetModified();
                            }
                            else
                            {
                                row.SetAdded();
                            }
                        }
                        CDArchiveService.SaveData(archiveMangeModal);
                        break;
                    case "3": //科学技术研究类 F
                        //archiveTableEnum = ArchiveTableEnum.Technology;
                        int maxOrder = 0;
                        UpdateArchiveAndPageFileModel<TechnologyArchiveEntity, PageFileEntity> model2 = Newtonsoft.Json.JsonConvert.DeserializeObject<UpdateArchiveAndPageFileModel<TechnologyArchiveEntity, PageFileEntity>>(requestData);
                        ITechArchiveService TechArchiveService = UContainer.Resolve<ITechArchiveService>();
                        foreach (TechnologyArchiveEntity entity in model2.Archives)
                        {
                            ArchiveMangeModal.TECHNOLOGY_ARCHIVERow row = archiveMangeModal.TECHNOLOGY_ARCHIVE.NewTECHNOLOGY_ARCHIVERow();
                            TechnologyArchiveEntityHelper.EntityToRow(entity, row);
                            row.CREATEUSERID = userId;
                            row.CREATETIME = DateTime.Now;
                            row.FULLFONDSNUM = orgFullFonds;
                            if (!string.IsNullOrEmpty(entity.ArchiveNum))
                            {
                                row.STATUSID = new ArchiveCacheFacade().GetStatusRuleCache().GetFinishStatus(int.Parse(catalogId));
                            }
                            else
                            {
                                //row.STATUSID = new WorkFlowManager().GetOriginalStastuIDByTableID(archiveTableEnum);
                                row.STATUSID = 0;
                                entityIds.Add(new Guid(entity.DataId));
                            }
                            archiveMangeModal.TECHNOLOGY_ARCHIVE.AddTECHNOLOGY_ARCHIVERow(row);
                            if (entity.DataRowOpMode == "Update")
                            {
                                updates.Add(entity.DataId);
                            }
                           
                        }
                        //根据卷ID查询最大的序号
                        Dictionary<string, int> PageFileOrder = new Dictionary<string, int>();
                        foreach (PageFileEntity entity in model2.PageFiles)
                        {
                            
                            ArchiveMangeModal.PAGEFILERow row = archiveMangeModal.PAGEFILE.NewPAGEFILERow();
                            PageFileEntityHelper.EntityToRow(entity, row);
                            row.CREATEUSERID = userId;
                            row.CREATETIME = DateTime.Now;
                            row.FULLFONDSNUM = orgFullFonds;
                            //根据类别获取卷内件序号
                            IPageFileService pagefile = UContainer.Resolve<IPageFileService>();
                            maxOrder = pagefile.GetPageFileMaxOrder(entity.F_ArchiveId)>=maxOrder? pagefile.GetPageFileMaxOrder(entity.F_ArchiveId):maxOrder;
                            //如果字典中没存储pagefile的最大序号，则添加进去并且将最大序号加1
                            if (!PageFileOrder.ContainsKey(entity.F_ArchiveId))
                            {
                                PageFileOrder.Add(entity.F_ArchiveId, maxOrder++);
                            }
                                //如果字典中存储了最大的序号，择将指定的卷内件序号+1
                            else
                            {
                                PageFileOrder[entity.F_ArchiveId] = maxOrder++;
                            }
                            //从字典中读取最大序号
                            row.ORDERNUM = PageFileOrder[entity.F_ArchiveId];
                            archiveMangeModal.PAGEFILE.AddPAGEFILERow(row);
                        }
                        maxOrder = 0;
                        archiveMangeModal.AcceptChanges();
                        foreach (ArchiveMangeModal.TECHNOLOGY_ARCHIVERow row in archiveMangeModal.TECHNOLOGY_ARCHIVE.Rows)
                        {
                            if (updates.Contains(row.DATAID))
                            {
                                row.SetModified();
                            }
                            else
                            {
                                row.SetAdded();
                            }
                        }
                        foreach (ArchiveMangeModal.PAGEFILERow row in archiveMangeModal.PAGEFILE.Rows)
                        {
                            row.SetAdded();
                        }
                        TechArchiveService.SaveData(archiveMangeModal);
                        break;
                    case "4": //建设项目类 G
                        //archiveTableEnum = ArchiveTableEnum.BulidProject;
                        UpdateArchiveAndPageFileModel<BulidProjectArchiveEntity, PageFileEntity> model8 = Newtonsoft.Json.JsonConvert.DeserializeObject<UpdateArchiveAndPageFileModel<BulidProjectArchiveEntity, PageFileEntity>>(requestData);
                        IBuildProjectArchiveService BuildProjectArchiveService = UContainer.Resolve<IBuildProjectArchiveService>();
                        foreach (BulidProjectArchiveEntity entity in model8.Archives)
                        {
                            ArchiveMangeModal.BULIDPROJECT_ARCHIVERow row = archiveMangeModal.BULIDPROJECT_ARCHIVE.NewBULIDPROJECT_ARCHIVERow();
                            BulidProjectArchiveEntityHelper.EntityToRow(entity, row);
                            row.CREATEUSERID = userId;
                            row.CREATETIME = DateTime.Now;
                            row.FULLFONDSNUM = orgFullFonds;
                            if (!string.IsNullOrEmpty(entity.ArchiveNum))
                            {
                                row.STATUSID = new ArchiveCacheFacade().GetStatusRuleCache().GetFinishStatus(int.Parse(catalogId));
                            }
                            else
                            {
                                //row.STATUSID = new WorkFlowManager().GetOriginalStastuIDByTableID(archiveTableEnum);
                                row.STATUSID = 0;
                                entityIds.Add(new Guid(entity.DataId));
                            }
                            archiveMangeModal.BULIDPROJECT_ARCHIVE.AddBULIDPROJECT_ARCHIVERow(row);
                            if (entity.DataRowOpMode == "Update")
                            {
                                updates.Add(entity.DataId);
                            }
                        }
                        foreach (PageFileEntity entity in model8.PageFiles)
                        {
                            ArchiveMangeModal.PAGEFILERow row = archiveMangeModal.PAGEFILE.NewPAGEFILERow();
                            PageFileEntityHelper.EntityToRow(entity, row);
                            row.CREATEUSERID = userId;
                            row.CREATETIME = DateTime.Now;
                            row.FULLFONDSNUM = orgFullFonds;
                            archiveMangeModal.PAGEFILE.AddPAGEFILERow(row);
                        }
                        archiveMangeModal.AcceptChanges();
                        foreach (ArchiveMangeModal.BULIDPROJECT_ARCHIVERow row in archiveMangeModal.BULIDPROJECT_ARCHIVE.Rows)
                        {
                            if (updates.Contains(row.DATAID))
                            {
                                row.SetModified();
                            }
                            else
                            {
                                row.SetAdded();
                            }
                        }
                        foreach (ArchiveMangeModal.PAGEFILERow row in archiveMangeModal.PAGEFILE.Rows)
                        {
                            row.SetAdded();
                        }
                        BuildProjectArchiveService.SaveData(archiveMangeModal);
                        break;
                    case "5": //设备仪器类 H
                        //archiveTableEnum = ArchiveTableEnum.Equipment;
                        UpdateArchiveAndPageFileModel<EquipmentArchiveEntity, PageFileEntity> model7 = Newtonsoft.Json.JsonConvert.DeserializeObject<UpdateArchiveAndPageFileModel<EquipmentArchiveEntity, PageFileEntity>>(requestData);
                        IEquipmentService EquipmentService = UContainer.Resolve<IEquipmentService>();
                        foreach (EquipmentArchiveEntity entity in model7.Archives)
                        {
                            ArchiveMangeModal.EQUIPMENT_ARCHIVERow row = archiveMangeModal.EQUIPMENT_ARCHIVE.NewEQUIPMENT_ARCHIVERow();
                            EquipmentArchiveEntityHelper.EntityToRow(entity, row);
                            row.CREATEUSERID = userId;
                            row.CREATETIME = DateTime.Now;
                            row.FULLFONDSNUM = orgFullFonds;
                            if (!string.IsNullOrEmpty(entity.ArchiveNum))
                            {
                                row.STATUSID = new ArchiveCacheFacade().GetStatusRuleCache().GetFinishStatus(int.Parse(catalogId));
                            }
                            else
                            {
                                //row.STATUSID = new WorkFlowManager().GetOriginalStastuIDByTableID(archiveTableEnum);
                                row.STATUSID = 0;
                                entityIds.Add(new Guid(entity.DataId));
                            }
                            archiveMangeModal.EQUIPMENT_ARCHIVE.AddEQUIPMENT_ARCHIVERow(row);
                            if (entity.DataRowOpMode == "Update")
                            {
                                updates.Add(entity.DataId);
                            }
                        }
                        foreach (PageFileEntity entity in model7.PageFiles)
                        {
                            ArchiveMangeModal.PAGEFILERow row = archiveMangeModal.PAGEFILE.NewPAGEFILERow();
                            PageFileEntityHelper.EntityToRow(entity, row);
                            row.CREATEUSERID = userId;
                            row.CREATETIME = DateTime.Now;
                            row.FULLFONDSNUM = orgFullFonds;
                            archiveMangeModal.PAGEFILE.AddPAGEFILERow(row);
                        }
                        archiveMangeModal.AcceptChanges();
                        foreach (ArchiveMangeModal.EQUIPMENT_ARCHIVERow row in archiveMangeModal.EQUIPMENT_ARCHIVE.Rows)
                        {
                            if (updates.Contains(row.DATAID))
                            {
                                row.SetModified();
                            }
                            else
                            {
                                row.SetAdded();
                            }
                        }
                        foreach (ArchiveMangeModal.PAGEFILERow row in archiveMangeModal.PAGEFILE.Rows)
                        {
                            row.SetAdded();
                        }
                        EquipmentService.SaveData(archiveMangeModal);
                        break;
                    case "61": //会计凭证 I01
                    case "62": //会计账簿 I02
                    case "63": //财务报告 I03
                    case "64": //其他 I04
                        //archiveTableEnum = ArchiveTableEnum.Account;
                        UpdateArchiveAndPageFileModel<AccountVoucherArchiveEntity, PageFileEntity> model3 = Newtonsoft.Json.JsonConvert.DeserializeObject<UpdateArchiveAndPageFileModel<AccountVoucherArchiveEntity, PageFileEntity>>(requestData);
                        IAccountArchiveService AccountArchiveService = UContainer.Resolve<IAccountArchiveService>();
                        foreach (AccountVoucherArchiveEntity entity in model3.Archives)
                        {
                            ArchiveMangeModal.ACCOUNT_VOUCHER_ARCHIVERow row = archiveMangeModal.ACCOUNT_VOUCHER_ARCHIVE.NewACCOUNT_VOUCHER_ARCHIVERow();
                            AccountVoucherArchiveEntityHelper.EntityToRow(entity, row);
                            row.CREATEUSERID = userId;
                            row.CREATETIME = DateTime.Now;
                            row.FULLFONDSNUM = orgFullFonds;
                            if (!string.IsNullOrEmpty(entity.ArchiveNum))
                            {
                                row.STATUSID = new ArchiveCacheFacade().GetStatusRuleCache().GetFinishStatus(int.Parse(catalogId));
                            }
                            else
                            {
                                //row.STATUSID = new WorkFlowManager().GetOriginalStastuIDByTableID(archiveTableEnum);
                                row.STATUSID = 0;
                                entityIds.Add(new Guid(entity.DataId));
                            }
                            //archiveMangeModal.ACCOUNT_VOUCHER_ARCHIVE.AddACCOUNT_VOUCHER_ARCHIVERow(row);
                            archiveMangeModal.ACCOUNT_VOUCHER_ARCHIVE.Rows.Add(row);
                            if (entity.DataRowOpMode == "Update")
                            {
                                updates.Add(entity.DataId);
                            }
                        }
                        foreach (PageFileEntity entity in model3.PageFiles)
                        {
                            ArchiveMangeModal.PAGEFILERow row = archiveMangeModal.PAGEFILE.NewPAGEFILERow();
                            PageFileEntityHelper.EntityToRow(entity, row);
                            row.CREATEUSERID = userId;
                            row.CREATETIME = DateTime.Now;
                            row.FULLFONDSNUM = orgFullFonds;
                            archiveMangeModal.PAGEFILE.AddPAGEFILERow(row);
                        }
                        archiveMangeModal.AcceptChanges();
                        foreach (ArchiveMangeModal.ACCOUNT_VOUCHER_ARCHIVERow row in archiveMangeModal.ACCOUNT_VOUCHER_ARCHIVE.Rows)
                        {
                            if (updates.Contains(row.DATAID))
                            {
                                row.SetModified();
                            }
                            else
                            {
                                row.SetAdded();
                            }
                        }
                        foreach (ArchiveMangeModal.PAGEFILERow row in archiveMangeModal.PAGEFILE.Rows)
                        {
                            row.SetAdded();
                        }
                        AccountArchiveService.SaveData(archiveMangeModal);
                        break;
                    case "71": //勘探综合 K01
                    case "72": //勘探单井 K02
                        //archiveTableEnum = ArchiveTableEnum.OliSearch;
                        UpdateArchiveAndPageFileModel<OliSearchArchiveEntity, PageFileEntity> model4 = Newtonsoft.Json.JsonConvert.DeserializeObject<UpdateArchiveAndPageFileModel<OliSearchArchiveEntity, PageFileEntity>>(requestData);
                        IOilSearchArchiveService OilSearchArchiveService = UContainer.Resolve<IOilSearchArchiveService>();
                        foreach (OliSearchArchiveEntity entity in model4.Archives)
                        {
                            ArchiveMangeModal.OLISEARCH_ARCHIVERow row = archiveMangeModal.OLISEARCH_ARCHIVE.NewOLISEARCH_ARCHIVERow();
                            OliSearchArchiveEntityHelper.EntityToRow(entity, row);
                            row.CREATEUSERID = userId;
                            row.CREATETIME = DateTime.Now;
                            row.FULLFONDSNUM = orgFullFonds;
                            if (!string.IsNullOrEmpty(entity.ArchiveNum))
                            {
                                row.STATUSID = new ArchiveCacheFacade().GetStatusRuleCache().GetFinishStatus(int.Parse(catalogId));
                            }
                            else
                            {
                                //row.STATUSID = new WorkFlowManager().GetOriginalStastuIDByTableID(archiveTableEnum);
                                row.STATUSID = 0;
                                entityIds.Add(new Guid(entity.DataId));
                            }
                            archiveMangeModal.OLISEARCH_ARCHIVE.AddOLISEARCH_ARCHIVERow(row);
                            if (entity.DataRowOpMode == "Update")
                            {
                                updates.Add(entity.DataId);
                            }
                        }
                        //foreach (PageFileEntity entity in model4.PageFiles)
                        //{
                        //    ArchiveMangeModal.PAGEFILERow row = archiveMangeModal.PAGEFILE.NewPAGEFILERow();
                        //    PageFileEntityHelper.EntityToRow(entity, row);
                        //    row.CREATEUSERID = userId;
                        //    row.CREATETIME = DateTime.Now;
                        //    row.FULLFONDSNUM = orgFullFonds;
                        //    archiveMangeModal.PAGEFILE.AddPAGEFILERow(row);
                        //}
                        archiveMangeModal.AcceptChanges();
                        foreach (ArchiveMangeModal.OLISEARCH_ARCHIVERow row in archiveMangeModal.OLISEARCH_ARCHIVE.Rows)
                        {
                            if (updates.Contains(row.DATAID))
                            {
                                row.SetModified();
                            }
                            else
                            {
                                row.SetAdded();
                            }
                        }
                        //foreach (ArchiveMangeModal.PAGEFILERow row in archiveMangeModal.PAGEFILE.Rows)
                        //{
                        //    row.SetAdded();
                        //}
                        OilSearchArchiveService.SaveData(archiveMangeModal);
                        break;
                    case "81": //声像
                    case "82": //声像
                    case "83": //声像
                        //archiveTableEnum = ArchiveTableEnum.AudioVedio;
                        UpdateArchiveAndPageFileModel<AudioVedioArchiveEntity, PageFileEntity> model5 = Newtonsoft.Json.JsonConvert.DeserializeObject<UpdateArchiveAndPageFileModel<AudioVedioArchiveEntity, PageFileEntity>>(requestData);
                        IAudioArchiveService AudioArchiveService = UContainer.Resolve<IAudioArchiveService>();
                        foreach (AudioVedioArchiveEntity entity in model5.Archives)
                        {
                            ArchiveMangeModal.AUDIOVEDIO_ARCHIVERow row = archiveMangeModal.AUDIOVEDIO_ARCHIVE.NewAUDIOVEDIO_ARCHIVERow();
                            AudioVedioArchiveEntityHelper.EntityToRow(entity, row);
                            row.CREATEUSERID = userId;
                            row.CREATETIME = DateTime.Now;
                            row.FULLFONDSNUM = orgFullFonds;
                            if (!string.IsNullOrEmpty(entity.ArchiveNum))
                            {
                                row.STATUSID = new ArchiveCacheFacade().GetStatusRuleCache().GetFinishStatus(int.Parse(catalogId));
                            }
                            else
                            {
                                //row.STATUSID = new WorkFlowManager().GetOriginalStastuIDByTableID(archiveTableEnum);
                                row.STATUSID = 0;
                                entityIds.Add(new Guid(entity.DataId));
                            }
                            archiveMangeModal.AUDIOVEDIO_ARCHIVE.AddAUDIOVEDIO_ARCHIVERow(row);
                            if (entity.DataRowOpMode == "Update")
                            {
                                updates.Add(entity.DataId);
                            }
                        }
                        archiveMangeModal.AcceptChanges();
                        foreach (ArchiveMangeModal.AUDIOVEDIO_ARCHIVERow row in archiveMangeModal.AUDIOVEDIO_ARCHIVE.Rows)
                        {
                            if (updates.Contains(row.DATAID))
                            {
                                row.SetModified();
                            }
                            else
                            {
                                row.SetAdded();
                            }
                        }
                        AudioArchiveService.SaveData(archiveMangeModal);
                        break;
                    case "91": //荣誉 R01
                        //archiveTableEnum = ArchiveTableEnum.RealKind;
                        UpdateArchiveAndPageFileModel<RealKindArchiveEntity, PageFileEntity> model6 = JsonConvert.DeserializeObject<UpdateArchiveAndPageFileModel<RealKindArchiveEntity, PageFileEntity>>(requestData);
                        IRealKindArchiveService RealKindArchiveService = UContainer.Resolve<IRealKindArchiveService>();
                        foreach (RealKindArchiveEntity entity in model6.Archives)
                        {
                            ArchiveMangeModal.REALKIND_ARCHIVERow row = archiveMangeModal.REALKIND_ARCHIVE.NewREALKIND_ARCHIVERow();
                            RealKindArchiveEntityHelper.EntityToRow(entity, row);
                            row.CREATEUSERID = userId;
                            row.CREATETIME = DateTime.Now;
                            row.FULLFONDSNUM = orgFullFonds;
                            if (!string.IsNullOrEmpty(entity.ArchiveNum))
                            {
                                row.STATUSID = new ArchiveCacheFacade().GetStatusRuleCache().GetFinishStatus(int.Parse(catalogId));
                            }
                            else
                            {
                                //row.STATUSID = new WorkFlowManager().GetOriginalStastuIDByTableID(archiveTableEnum);
                                row.STATUSID = 0;
                                entityIds.Add(new Guid(entity.DataId));
                            }
                            archiveMangeModal.REALKIND_ARCHIVE.AddREALKIND_ARCHIVERow(row);
                            if (entity.DataRowOpMode == "Update")
                            {
                                updates.Add(entity.DataId);
                            }
                        }
                        archiveMangeModal.AcceptChanges();
                        foreach (ArchiveMangeModal.REALKIND_ARCHIVERow row in archiveMangeModal.REALKIND_ARCHIVE.Rows)
                        {
                            if (updates.Contains(row.DATAID))
                            {
                                row.SetModified();
                            }
                            else
                            {
                                row.SetAdded();
                            }
                        }
                        RealKindArchiveService.SaveData(archiveMangeModal);
                        break;
                    //Author:yidane
                    //Date:2014年4月12日22:57:38
                    case "101":
                        UpdateArchiveAndPageFileModel<DOCUMENT_ARCHIVE_VOL, PageFileEntity> model101 = JsonConvert.DeserializeObject<UpdateArchiveAndPageFileModel<DOCUMENT_ARCHIVE_VOL, PageFileEntity>>(requestData);
                        IDocumentArchiveVolService oIDocumentArchiveVolService = UContainer.Resolve<IDocumentArchiveVolService>();
                        foreach (DOCUMENT_ARCHIVE_VOL entity in model101.Archives)
                        {
                            ArchiveMangeModal.DOCUMENT_ARCHIVE_VOLRow row = archiveMangeModal.DOCUMENT_ARCHIVE_VOL.NewDOCUMENT_ARCHIVE_VOLRow();
                            DOCUMENT_ARCHIVE_VOLHelper.EntityToRow(entity, row);
                            row.CREATEUSERID = userId;
                            row.CREATETIME = DateTime.Now;
                            row.FULLFONDSNUM = orgFullFonds;
                            row.STATUSID = 0;

                            if (string.IsNullOrEmpty(entity.ArchiveNum))
                            {
                                entityIds.Add(new Guid(entity.DataId));
                            }
                            archiveMangeModal.DOCUMENT_ARCHIVE_VOL.AddDOCUMENT_ARCHIVE_VOLRow(row);
                            if (entity.DataRowOpMode == "Update")
                            {
                                updates.Add(entity.DataId);
                            }
                        }
                        archiveMangeModal.AcceptChanges();
                        foreach (ArchiveMangeModal.DOCUMENT_ARCHIVE_VOLRow row in archiveMangeModal.DOCUMENT_ARCHIVE_VOL)
                        {
                            if (updates.Contains(row.DATAID))
                            {
                                row.SetModified();
                            }
                            else
                            {
                                row.SetAdded();
                            }
                        }

                        oIDocumentArchiveVolService.SaveData(archiveMangeModal);
                        break;
                    case "102":
                        UpdateArchiveAndPageFileModel<DOCUMENT_ARCHIVE_VOL_File, PageFileEntity> model102 = JsonConvert.DeserializeObject<UpdateArchiveAndPageFileModel<DOCUMENT_ARCHIVE_VOL_File, PageFileEntity>>(requestData);
                        IDocumentArchiveVolFileService oIDocumentArchiveVolFileService = UContainer.Resolve<IDocumentArchiveVolFileService>();
                        foreach (DOCUMENT_ARCHIVE_VOL_File entity in model102.Archives)
                        {
                            ArchiveMangeModal.DOCUMENT_ARCHIVE_VOL_FILERow row = archiveMangeModal.DOCUMENT_ARCHIVE_VOL_FILE.NewDOCUMENT_ARCHIVE_VOL_FILERow();
                            DOCUMENT_ARCHIVE_VOL_FileHelper.EntityToRow(entity, row);
                            row.CREATEUSERID = userId;
                            row.CREATETIME = DateTime.Now;
                            row.FULLFONDSNUM = orgFullFonds;
                            row.STATUSID = 0;

                            if (string.IsNullOrEmpty(entity.ArchiveNum))
                            {
                                entityIds.Add(new Guid(entity.DataId));
                            }
                            archiveMangeModal.DOCUMENT_ARCHIVE_VOL_FILE.AddDOCUMENT_ARCHIVE_VOL_FILERow(row);
                            if (entity.DataRowOpMode == "Update")
                            {
                                updates.Add(entity.DataId);
                            }
                        }
                        archiveMangeModal.AcceptChanges();
                        foreach (ArchiveMangeModal.DOCUMENT_ARCHIVE_VOL_FILERow row in archiveMangeModal.DOCUMENT_ARCHIVE_VOL_FILE)
                        {
                            if (updates.Contains(row.DATAID))
                            {
                                row.SetModified();
                            }
                            else
                            {
                                row.SetAdded();
                            }
                        }

                        oIDocumentArchiveVolFileService.SaveData(archiveMangeModal);
                        break;
                    case "7101":
                        UpdateArchiveAndPageFileModel<OLISEARCH_ZongHe_VOL, PageFileEntity> model7101 = JsonConvert.DeserializeObject<UpdateArchiveAndPageFileModel<OLISEARCH_ZongHe_VOL, PageFileEntity>>(requestData);
                        IZongheVolService oIZongheVolService = UContainer.Resolve<IZongheVolService>();
                        foreach (OLISEARCH_ZongHe_VOL entity in model7101.Archives)
                        {
                            ArchiveMangeModal.OLISEARCH_ZONGHE_VOLRow row = archiveMangeModal.OLISEARCH_ZONGHE_VOL.NewOLISEARCH_ZONGHE_VOLRow();
                            OLISEARCH_ZongHe_VOLHelper.EntityToRow(entity, row);
                            row.CREATEUSERID = userId;
                            row.CREATETIME = DateTime.Now;
                            row.FULLFONDSNUM = orgFullFonds;
                            row.STATUSID = 0;

                            if (string.IsNullOrEmpty(entity.ArchiveNum))
                            {
                                entityIds.Add(new Guid(entity.DataId));
                            }
                            archiveMangeModal.OLISEARCH_ZONGHE_VOL.AddOLISEARCH_ZONGHE_VOLRow(row);
                            if (entity.DataRowOpMode == "Update")
                            {
                                updates.Add(entity.DataId);
                            }
                        }
                        archiveMangeModal.AcceptChanges();
                        foreach (ArchiveMangeModal.OLISEARCH_ZONGHE_VOLRow row in archiveMangeModal.OLISEARCH_ZONGHE_VOL)
                        {
                            if (updates.Contains(row.DATAID))
                            {
                                row.SetModified();
                            }
                            else
                            {
                                row.SetAdded();
                            }
                        }

                        oIZongheVolService.SaveData(archiveMangeModal);
                        break;
                    case "7102":
                        UpdateArchiveAndPageFileModel<OLISEARCH_ZongHe_VOL_FILE, PageFileEntity> model7102 = JsonConvert.DeserializeObject<UpdateArchiveAndPageFileModel<OLISEARCH_ZongHe_VOL_FILE, PageFileEntity>>(requestData);
                        IZongheVolFileService oIZongheVolFileService = UContainer.Resolve<IZongheVolFileService>();
                        foreach (OLISEARCH_ZongHe_VOL_FILE entity in model7102.Archives)
                        {
                            ArchiveMangeModal.OLISEARCH_ZONGHE_VOL_FILERow row = archiveMangeModal.OLISEARCH_ZONGHE_VOL_FILE.NewOLISEARCH_ZONGHE_VOL_FILERow();
                            OLISEARCH_ZongHe_VOL_FILEHelper.EntityToRow(entity, row);
                            row.CREATEUSERID = userId;
                            row.CREATETIME = DateTime.Now;
                            row.FULLFONDSNUM = orgFullFonds;
                            row.STATUSID = 0;

                            if (string.IsNullOrEmpty(entity.ArchiveNum))
                            {
                                entityIds.Add(new Guid(entity.DataId));
                            }
                            archiveMangeModal.OLISEARCH_ZONGHE_VOL_FILE.AddOLISEARCH_ZONGHE_VOL_FILERow(row);
                            if (entity.DataRowOpMode == "Update")
                            {
                                updates.Add(entity.DataId);
                            }
                        }
                        archiveMangeModal.AcceptChanges();
                        foreach (ArchiveMangeModal.OLISEARCH_ZONGHE_VOL_FILERow row in archiveMangeModal.OLISEARCH_ZONGHE_VOL_FILE)
                        {
                            if (updates.Contains(row.DATAID))
                            {
                                row.SetModified();
                            }
                            else
                            {
                                row.SetAdded();
                            }
                        }

                        oIZongheVolFileService.SaveData(archiveMangeModal);
                        break;
                    default:
                        throw new Exception("Type Error!");
                }
                //历史档案,则不启动流程
                List<string> list=new List<string>(){"101","102","7101","7102"};
                if (!list.Contains(catalogId))
                {
                    foreach (Guid id in entityIds)
                    {
                        this.FlowStart(id, new Guid(userId), orgFullFonds, catalogId);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void FlowStart(Guid recordID, Guid currentUserID, string orgFullFonds, string ReqArchiveType)
        {
            WorkFlowModel oWorkFlowModel = new WorkFlowModel();
            WorkFlow oWorkFlow = new WorkFlow();
            string operateName = "移交";
            int originaleStatusID;
            if (orgFullFonds == CommonUtil.GetConfigValue("ArchiveManage", "InstitutionFullFondsNum"))
            {
                originaleStatusID = new WorkFlowManager().GetStatusIdByTableIdAndStatusName(int.Parse(ReqArchiveType), string.Concat("机关-", "录入中"));
                operateName = string.Concat("机关-", operateName);
            }
            else
            {
                originaleStatusID = new WorkFlowManager().GetStatusIdByTableIdAndStatusName(int.Parse(ReqArchiveType), "录入中");
            }
            //oWorkFlowModel.CommentMessage = string.Empty;//获取评论，在上报时候，基本是没有评论的。
            oWorkFlowModel.TableID = int.Parse(ReqArchiveType);//获取表ID
            oWorkFlowModel.FromUserID = currentUserID;//获取当前用户ID
            oWorkFlowModel.RecordID = recordID;
            oWorkFlowModel.FromStatusID = originaleStatusID;
            oWorkFlowModel.OperateLogID = Guid.NewGuid();
            WorkFlowMessageStatusRuleListCache ruleCache = new ArchiveCacheFacade().GetStatusRuleCache();
            WorkFlowMessageStatusRule rule = ruleCache.GetRuleByStatusIDAndOperateName(oWorkFlowModel.FromStatusID, operateName);
            oWorkFlowModel.OperateID = rule.OperateID;//取驱动的ID
            oWorkFlow.FlowStart(oWorkFlowModel);//启动流程
        }
    }
}